package com.yix.admin.modules.system.repository;

import com.yix.admin.modules.system.entity.SysDept;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Set;

/**
 * @author wdx
 * @version 1.0
 * @Description TODO
 * @date 2021/2/2 14:04
 */
public interface SysDeptRepository extends JpaRepository<SysDept, String>, JpaSpecificationExecutor<SysDept> {

    /**
     * 根据角色ID 查询
     * @param roleId 角色ID
     * @return /
     */
    @Query(value = "select d.* from sys_dept d, roles_depts r where " +
            "d.id = r.dept_id and r.role_id = ?1", nativeQuery = true)
    Set<SysDept> findByRoleId(String roleId);

    /**
     * 根据 PID 查询
     * @param id pid
     * @return /
     */
    List<SysDept> findByPid(String id);

    /**
     * 获取顶级部门
     * @return /
     */
    List<SysDept> findByPidIsNull();

    /**
     * 判断是否存在子节点
     * @param pid /
     * @return /
     */
    int countByPid(String pid);

    /**
     * 根据ID更新sub_count
     * @param count /
     * @param id /
     */
    @Modifying
    @Query(value = " update sys_dept set sub_count = ?1 where id = ?2 ",nativeQuery = true)
    void updateSubCntById(Integer count, String id);
}
